package com.hui.xiao.xiaohui.view;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

import com.hui.xiao.xiaohui.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/20.
 */
public class GridViewTest extends Activity {
    GridView gridView;
    ImageView imageView;
    int[] imageIds = new int[]{
            R.drawable.bkch, R.drawable.bkch, R.drawable.bkch, R.drawable.bkch,
            R.drawable.bkch, R.drawable.aboc, R.drawable.bkch, R.drawable.bkch
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_view);
        List<Map<String, Object>> items = new ArrayList<>();
        for (int i = 0; i < imageIds.length; i++) {
            Map<String, Object> item = new HashMap<>();
            item.put("image", imageIds[i]);
            items.add(item);
        }
        imageView = (ImageView) findViewById(R.id.imageView);
        SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.cell, new String[]{"image"}, new int[]{R.id.image1});
        gridView = (GridView) findViewById(R.id.grid01);
        gridView.setAdapter(adapter);
        gridView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                imageView.setImageResource(imageIds[position]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                imageView.setImageResource(imageIds[position]);
            }
        });
    }
}
